<template>
    <div class="survey container-content">
        <div class="app-view-box">
            <el-row>
                <el-col :span="24">
                    <h1 class="panel-title">报价单</h1>
                </el-col>
            </el-row>
            <el-divider class="divider"></el-divider>
            <el-row class="export-container">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="addNewData"
                    >新增</el-button
                >
                <el-button
                    type="primary"
                    icon="el-icon-download"
                    size="mini"
                    @click="exporting"
                    >导出</el-button
                >
            </el-row>
            <el-table
                ref="customerTable"
                :data="tableData"
                border
                stripe
                style="width: 100%"
                :summary-method="getSummaries"
                show-summary
            >
                <!-- @selection-change="handleSelectionChange" -->
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    type="index"
                    label="序号"
                    width="55"
                    align="center"
                />
                <el-table-column prop="docCode" label="单据编号">
                    <template slot-scope="scope">
                        {{ scope.row.docCode || "--" }}
                    </template>
                </el-table-column>

                <el-table-column prop="docStatus" label="单据状态">
                    <template slot-scope="scope">
                        {{ scope.row.docStatus || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="askTime" label="报价日期">
                    <template slot-scope="scope">
                        {{ scope.row.askTime || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="materialCode" label="物料编码">
                    <template slot-scope="scope">
                        {{ scope.row.materialCode || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="materialName" label="物料名称">
                    <template slot-scope="scope">
                        {{ scope.row.materialName || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="supplierCode" label="供应商编码">
                    <template slot-scope="scope">
                        {{ scope.row.supplierCode || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="comfirmPrice" label="确认单价">
                    <template slot-scope="scope">
                        {{ scope.row.comfirmPrice || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="comfirmTaxPrice" label="确认含税单价">
                    <template slot-scope="scope">
                        {{ scope.row.comfirmTaxPrice || "--" }}
                    </template>
                </el-table-column>
                <el-table-column prop="comfirmComment" label="确认意见">
                    <template slot-scope="scope">
                        {{ scope.row.comfirmComment || "--" }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <el-tooltip
                            class="item"
                            effect="dark"
                            content="详情"
                            placement="bottom"
                        >
                            <el-button
                                plain
                                size="small"
                                icon="el-icon-more"
                                @click="handleDetail(scope.$index, scope.row)"
                            ></el-button>
                        </el-tooltip>
                        <el-tooltip
                            class="item"
                            effect="dark"
                            content="编辑"
                            placement="bottom"
                        >
                            <el-button
                                type="primary"
                                plain
                                icon="el-icon-edit"
                                size="small"
                                @click="handleEdit(scope.$index, scope.row)"
                            ></el-button>
                        </el-tooltip>
                        <el-tooltip
                            class="item"
                            effect="dark"
                            content="删除"
                            placement="bottom"
                        >
                            <el-button
                                type="danger"
                                plain
                                size="small"
                                icon="el-icon-delete"
                                @click="handleDelete(scope.$index, scope.row)"
                            ></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-pagination
            class="table-pagination mt10"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            background
        >
        </el-pagination>
    </div>
</template>
<script>
import { getOfferList, deleteList } from "@/api/system/purchase";

export default {
    data() {
        return {
            searchValue: "",
            total: 0,
            tableData: [],
            limit: 10,
            page: 1,
        };
    },
    methods: {
        addNewData() {},
        exporting() {},
        // 获取列表请求
        getData() {
            let params = {
                limit: this.limit,
                page: this.page,
            };
            getOfferList(params).then((res) => {
                if (res.code === 200) {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                }
            });
        },
        // 删除事件
        handleDelete(index, row) {
            this.$confirm("请确定要删除所选的记录吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    deleteList(row.id).then((res) => {
                        if (res.code === 200) {
                            this.tableData.splice(index, 1);
                            this.$message({
                                type: "success",
                                message: "删除成功!",
                            });
                            this.getData();
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        // 搜索事件
        searchData() {
            // let searchValue = this.searchValue;
            // searchSurvey({ searchValue }).then((res) => {
            //   this.tableData = res.rows;
            //   console.log("请求成功", res);
            // });
        },
        //新增按钮
        addNewData() {
            this.$store.dispatch("workflow/getModel", {
                menuId: this.$route.meta.menuId,
                type: "launch",
                businessType: "35",
            });
        },
        //编辑按钮
        handleEdit(index, row) {
            // let data = JSON.stringify(row);
            // this.$router.push({ path: "/Survey/EditData", query: { data } });
        },
        // 详情按钮
        handleDetail(index, row) {
            // this.$router.push({ path: "/Survey/DetailData" });
        },
        //分页器size
        handleSizeChange(val) {
            this.limit = val;
            this.getData();
        },
        //分页器当前页
        handleCurrentChange(val) {
            this.page = val;
            this.getData();
        },
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "总计";
                    return;
                }
                if (column.property === "inquiryNum") {
                    const values = data.map((item) =>
                        Number(item[column.property])
                    );
                    if (!values.every((value) => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += "";
                    } else {
                        sums[index] = "N/A";
                    }
                } else {
                    return;
                }
            });

            return sums;
        },
    },
    created() {
        this.getData();
    },
};
</script>
<style lang="scss" scoped>
.divider {
    margin: 0;
}

.export-container {
    margin: 15px 0;
}

.pagination {
    margin-top: 10px;
}
</style>
